﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CApp_Starts
{
    public partial class Form1 : Form
    {



        public Form1()
        {

            InitializeComponent();

            this.pan_block.Width = Consts.G_W * Consts.V_W + 1;
            this.pan_block.Height = Consts.G_H * Consts.V_H + 1;

        }

        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    Graphics grap = this.pan_block.CreateGraphics();
        //    Pen red = new Pen(Color.Red, 1);
        //    for (int i = 0; i < this.pan_block.Height; i++)
        //    {
        //        grap.DrawLine(red, 0, i * 20, this.pan_block.Width, i * 20);
        //    }

        //    for (int k = 0; k < this.pan_block.Width; k++)
        //    {
        //        grap.DrawLine(red, k * 20, 0, k * 20, this.pan_block.Height);
        //    }
        //}


        bool start = false;

        Thread thread = null;
        private void button1_Click(object sender, EventArgs e)
        {
            start = true;
            thread = new Thread(() =>
            {
                try
                {
                    Graphics grap = this.pan_block.CreateGraphics();

                    Pen red = new Pen(Color.Red, 1);

                    Grid gridAdd = new Grid(5, 5);

                    Consts.grids[gridAdd.XY] = gridAdd;

                    grap.FillRectangle(Brushes.Black, 0, 0, this.pan_block.Width, this.pan_block.Height);

                    while (start)
                    {
                        try
                        {
                            Thread.Sleep(20);

                            for (int i = 0; i < this.pan_block.Height; i++)
                            {
                                grap.DrawLine(red, 0, i * 20, this.pan_block.Width, i * 20);
                            }

                            for (int k = 0; k < this.pan_block.Width; k++)
                            {
                                grap.DrawLine(red, k * 20, 0, k * 20, this.pan_block.Height);
                            }

                            for (int i = 0; i < Consts.G_H; i++)
                            {
                                for (int k = 0; k < Consts.G_W; k++)
                                {
                                    Grid grid = Consts.grids[i * k];
                                    if (grid != null && grid.X >= 0 && grid.Y >= 0)
                                    {
                                        grap.FillRectangle(Brushes.Red, k * Consts.V_W, i * Consts.V_H, Consts.V_W, Consts.V_H);
                                    }
                                }
                            }

                            if (Form1.XiaJiang(true))
                            {
                                /*设置不能移动*/
                                for (int i = Consts.G_H - 2; i >= 0; i--)
                                {
                                    for (int k = Consts.G_W - 1; k >= 0; k--)
                                    {
                                        Grid grid = Consts.grids[i * k];
                                        if (grid != null)
                                        {
                                            grid.YunDong = false;
                                        }
                                    }
                                }
                            }

                            /*消除*/
                            for (int i = Consts.G_H - 1; i >= 0; i--)
                            {
                                for (int k = Consts.G_W - 1; k >= 0; k--)
                                {
                                    Grid grid = Consts.grids[i * k];
                                    if (grid == null)
                                    {
                                        goto lab_001;
                                    }
                                }

                                for (int k = Consts.G_W - 1; k >= 0; k--)
                                {
                                    Consts.grids[i * k] = null;
                                }

                            }

                        lab_001:
                            Form1.XiaJiang(true);

                        }
                        catch (Exception)
                        {

                        }

                    }

                    grap.DrawString("结束", new Font("宋体", 15), Brushes.Red, 50, 50);

                    grap.Dispose();
                }
                catch (Exception)
                {

                }

            });

            thread.IsBackground = true;
            thread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            start = false;
            thread.Join();
        }


        static public bool XiaJiang(bool yanz)
        {
            for (int i = Consts.G_H - 2; i >= 0; i--)
            {
                for (int k = Consts.G_W - 1; k >= 0; k--)
                {
                    Grid grid = Consts.grids[i * k];
                    if (grid != null && grid.YunDong == yanz)
                    {
                        Grid grid1 = Consts.grids[(i + 1) * k];
                        if (grid1 != null)
                        {
                            /*判断格子是否还能往下移动*/
                            return false;
                        }
                    }
                }

                /*把格子往下移动*/
                for (int k = Consts.G_W - 1; k >= 0; k--)
                {
                    Grid grid = Consts.grids[i * k];
                    if (grid != null && grid.YunDong == yanz)
                    {
                        Consts.grids[i * k] = null;
                        grid.X = k;
                        grid.Y = i + 1;
                        Consts.grids[grid.XY] = grid;
                    }
                }
            }
            return true;
        }



    }
}
